//
//  SelectPhotoTool.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SelectPhotoTool.h"
#import "SelectPhotoModel.h"
#import "UIImage+DP.h"

@implementation SelectPhotoTool
// 获取最新照片时间
+ (void)newestDateWithModelArray:(NSMutableArray *)modelArray completion:(void (^)(NSDate *newestDate, BOOL isSameDay))completion
{
    BOOL isSame = YES;
    NSDate *date;
    SelectPhotoModel *model = modelArray.firstObject;
    NSDate *newestDate = model.imageDate;
    for (NSInteger i = 1; i < modelArray.count; i++) {
        model = [modelArray objectAtIndex:i];
        
        // 判断是否同一天
        if (![newestDate yj_isEqualToDateIgnoreTime:model.imageDate]) {
            // 不是同一天
            isSame = NO;
        }
        
        // 判断最新那天
        if (!newestDate || [newestDate isEarlierThanDate:date]) {
            newestDate = date;
        }
        
    }
    // 防止为nil
    if (!newestDate) {
        newestDate = [NSDate date];
    }
    
    // 回调
    if (completion) {
        completion(newestDate, isSame);
    }
}

/**
 *  把ALAsset数据的数组转为DPPostImageModel的数据数组
 *
 *  @param assetsArray ALAsset数组
 *
 *  @return DPPostImageModel的数据数组
 */
+ (NSMutableArray *)getPostImageModelArrayWithAssetsArray:(NSMutableArray *)AssetsArray
{
//    ALAsset *asset = nil;
//    UIImage *thumbImage = nil;
//    UIImage *uploadImage = nil;
    SelectPhotoModel *model = nil;
    BMPhotoIOPhotoModel *photoIOModel = nil;
//    NSDate *date;
    NSMutableArray *modelArray = [NSMutableArray array];
    for (NSInteger i = 0; i < AssetsArray.count; i++) {
        photoIOModel = [AssetsArray objectAtIndex:i];
//        asset = photoIOModel.assetIOS7;
//        thumbImage = [UIImage imageWithCGImage:asset.thumbnail];
//        UIImage *originalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[[asset defaultRepresentation] orientation]];
//        uploadImage = [originalImage getUploadImage];
//        date = [asset valueForProperty:ALAssetPropertyDate];
//        model = [[SelectPhotoModel alloc] initWithImage:thumbImage imageDate:date uploadImage:uploadImage];
//        [modelArray addObject:model];
        model = [[SelectPhotoModel alloc] initWithImage:photoIOModel.thumbImage imageDate:photoIOModel.createDate uploadImage:[photoIOModel.thumbImage zipImage]];
        [modelArray addObject:model];
    }
    return modelArray;
}
@end
